package elijah.base.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;

import androidx.annotation.Nullable;

import java.util.Locale;

public class ArcProcessView extends View {


    //数值字体
    private float valueTextSize = 22;
    //标签字体
    private float labelTextSize = 12;
    //完成率
    private float rate = 0.755f;
    //完成率格式化
    private final String labelFormat = "%.1f%%";
    //标签文本
    private String label = "周达成率";

    //进度条宽度
    private int barWidth = 20;
    private int sweepAngle = 260;
    private final Paint paint;
    private TextPaint textPaint;
    private int width, height;

    private final RectF rectF;
    private final SweepGradient sweepGradient = new SweepGradient(0, 0, new int[]{0xff217bf5, 0xff26c9c3, 0xff28e9ab}, new float[]{0, 0.7f, 1});
    private RadialGradient radialGradient = new RadialGradient(0, 0, 35, new int[]{0x3521f7ff, 0x2421f7ff, 0x1121f7ff, 0x0021f7ff}, new float[]{0, 0.65f, 0.82f, 1}, Shader.TileMode.CLAMP);

    private final LinearGradient linearGradient = new LinearGradient(0, 10, 0, -30, new int[]{0x0032a8f0, 0x0132a8f0, 0x1132a8f0, 0xff32a8f0}, new float[]{0, 0.1f, 0.3f, 1}, Shader.TileMode.CLAMP);


    public ArcProcessView setRate(float rate) {
        this.rate = rate;
        postInvalidate();
        return this;
    }

    public ArcProcessView setLabel(String label) {
        this.label = label;
        postInvalidate();
        return this;

    }

    public ArcProcessView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                radialGradient = new RadialGradient(0, 0, width / 2f - barWidth * 2f, new int[]{0x3521f7ff, 0x2421f7ff, 0x1121f7ff, 0x0021f7ff}, new float[]{0, 0.65f, 0.82f, 1}, Shader.TileMode.CLAMP);
            }
        });


        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        rectF = new RectF();

        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setFakeBoldText(true);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setShader(null);

        canvas.translate(width / 2f, height / 2f);

        paint.setStrokeWidth(barWidth);
        paint.setStyle(Paint.Style.STROKE);
        rectF.set(-width / 2f + barWidth, -height / 2f + barWidth, width / 2f - barWidth, height / 2f - barWidth);


        float startAngle = (360 - sweepAngle) / 2f + 90;

        //左脚
        paint.setColor(0xffffffff);
        canvas.drawArc(rectF, startAngle - 1.5f, 1.5f, false, paint);

        //背景
        paint.setColor(0xff202d5d);
        canvas.drawArc(rectF, startAngle, sweepAngle, false, paint);
        //右脚
        paint.setColor(0xff00e3f3);
        canvas.drawArc(rectF, startAngle + sweepAngle, 1.5f, false, paint);

        //进度
        paint.setShader(sweepGradient);
        canvas.drawArc(rectF, startAngle, sweepAngle * rate, false, paint);
        //进度末端蓝条
        if (sweepAngle * rate > 4) {
            paint.setShader(null);
            paint.setColor(0xff00f1ff);
            canvas.drawArc(rectF, startAngle + sweepAngle * rate - 4, 4, false, paint);
        }


        //半弧线
        paint.setShader(null);
        paint.setStrokeWidth(1.5f);
        paint.setShader(linearGradient);
        rectF.set(-width / 2f + barWidth * 1.5f + 6, -height / 2f + barWidth * 1.5f + 6, width / 2f - barWidth * 1.5f - 6, height / 2f - barWidth * 1.5f - 6);
        canvas.drawArc(rectF, startAngle - 10, sweepAngle - 20, false, paint);

        //文字背景
        paint.setStyle(Paint.Style.FILL);
        paint.setShader(radialGradient);

        canvas.drawCircle(0, 0, width / 2f - 40, paint);

        drawValue(canvas);

    }

    private void drawValue(Canvas canvas) {
        textPaint.setTextSize(valueTextSize);
        textPaint.setColor(0xff21f7ff);
        String text = String.format(Locale.CHINA, labelFormat, rate * 100);
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        float baseline = rectF.centerY() + distance;
        canvas.drawText(text, rectF.centerX(), baseline, textPaint);
        textPaint.setTextSize(labelTextSize);
        textPaint.setColor(0xffffffff);
        canvas.drawText(label, rectF.centerX(), baseline + valueTextSize * 1.2f, textPaint);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
//        height = MeasureSpec.getSize(height);
        height = width;
    }
}
